#!/usr/bin/env python
#!coding=utf-8
from threading import Thread
from greeevent import *
from greepeer import BaseGreecket,CollaboratePeer,Collaborate
from greenlet import greenlet
from time import sleep


class Greecket(BaseGreecket):
    def __init__(self,parent=None,collaborate=False):
        super(Greecket,self).__init__(collaborate)
        self._GreenletLoop=greenlet(self._greenletLoop)
        self._Running=False
        
    def _eventLoop(self):
        i=1
        while self._Running:
            self._GreenletLoop.switch()
            sleep(0.005)

    def pushEvent(self,event=None):
        if event.Peer.getEventBind(event.EventId) or self.getEventBind(event.EventId):
            t=Thread(target=self._eventproccess,args=(event,))
            t.setDaemon(True)
            t.start()

    @staticmethod
    def mainloop():
        if Greecket._factory:
            Greecket._factory._Running=True
            Greecket._factory._eventLoop()
            

mainloop=Greecket.mainloop()
        
